package com.lulin.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lulin.mall.product.entity.CategoryBrandRelationEntity;
import com.lulin.mall.product.service.CategoryBrandRelationService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;
import sun.rmi.runtime.Log;


/**
 * 品牌分类关联
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 14:25:13
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 根据类别id，查询对应的所有品牌信息
     *
     * @param catId 类别id 必输 默认为 0
     * @return
     */
    @GetMapping("/brands/list")
    public R categoryBranRelation(@RequestParam(value = "catId", required = true, defaultValue = "0") Long catId) {
        //根据类别id，查询对应的所有品牌信息
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.categoryBranRelation(catId);
        return R.ok().put("data", list);
    }

    /**
     * 查询品牌和类目管理的列表
     *
     * @param brandId 品牌的id
     * @return list列表
     */
    @RequestMapping("/catelog/list")
    public R catelogList(@RequestParam Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);

        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);

        //返回的数据，前端页面是data接收的
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        //categoryBrandRelationService.save(categoryBrandRelation);

        // 保存品牌关联三级类目
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
